using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace ProjectNoClue
{
    static class Input
    {

        //These are only here for testing purposes, until the entire team has a wired 360 controller.
        private static KeyboardState prevKeyboardState;
        private static KeyboardState currentKeyboardState;
        private static MouseState prevMouseState;
        private static MouseState currentMouseState;

        //These will always be here.
        private static GamePadState prevGamePadState = new GamePadState();
        private static GamePadState currentGamePadState = new GamePadState();

        //===============================================================================================================

        // In this area we will put all the controller mappings since you can't call the gamepad states directly from code elseware.
        // The functions below are more or less just test code, since we don't know 100% what our spaceships will do.

        //===============================================================================================================
        #region OMG CONTROLLER MAPPINGS

        public static bool OpenMap()
        {
            return (currentGamePadState.Buttons.Back == ButtonState.Pressed || currentKeyboardState.IsKeyDown(Keys.M));
        }

        public static bool Fire() 
        { 
            return (currentGamePadState.Buttons.A == ButtonState.Pressed || currentMouseState.LeftButton == ButtonState.Pressed);
        }
        public static bool FireClicked()
        {
            return (prevKeyboardState.IsKeyUp(Keys.Space) && currentKeyboardState.IsKeyDown(Keys.Space));
        }

        /// <summary>
        /// For now, this returns the x,y position of the left joystick. I expect you guys to figure out how fast the ship
        /// should move.
        /// </summary>
        /// <returns></returns>
        public static Vector2 Move()
        {
			if (currentGamePadState.IsConnected)
			{
				return (new Vector2(currentGamePadState.ThumbSticks.Left.X, -currentGamePadState.ThumbSticks.Left.Y));
			}
			else
			{
				Vector2 tempVec = new Vector2();
				if (currentKeyboardState.IsKeyDown(Keys.A))
					tempVec.X += -1;
				if (currentKeyboardState.IsKeyDown(Keys.D))
					tempVec.X += 1;
				if (currentKeyboardState.IsKeyDown(Keys.W))
					tempVec.Y += -1;
				if (currentKeyboardState.IsKeyDown(Keys.S))
					tempVec.Y += 1;

				Vector2.Normalize(ref tempVec, out tempVec);

				return tempVec;
			}
		}

        //Shouldn't this be a property? For some reason enrico didn't want properties. :(
        public static Vector2 MouseLocation
        {
            get
            {
                return (new Vector2(currentMouseState.X, currentMouseState.Y));
            }
        }


        #endregion


        //================================================================================================================
        static Input()
        {
            prevKeyboardState = Keyboard.GetState();
            currentKeyboardState = Keyboard.GetState();

            prevMouseState = Mouse.GetState();
            currentMouseState = Mouse.GetState();

            prevGamePadState = GamePad.GetState(PlayerIndex.One);
            currentGamePadState = GamePad.GetState(PlayerIndex.One);
        }

        /// <summary>
        /// Updates all of the gamepad states within this class.
        /// </summary>
        public static void Update()
        {


            prevMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();

            //REMARK: Maybe at some point we'll pull up a pause screen if GamePad has become unlplugged, and handle this stuff
            //differently, but for now I'll just assume no one is dumb enough to unplug their controller.
			if (GamePad.GetState(PlayerIndex.One).IsConnected == true)
			{
				prevGamePadState = currentGamePadState;
				currentGamePadState = GamePad.GetState(PlayerIndex.One);
			}
			else
			{
				prevKeyboardState = currentKeyboardState;
				currentKeyboardState = Keyboard.GetState();
			}
		}

    }

}
